// 计数器
function Counter(count, change, items, max) {
    this.count = +count;
    this.controls = [];
    this.items = items;
    this.itemMax = +max || 100;
    if (typeof change === 'function') {
        this.changeCb = change;
    }
    this.hasDis = this.getAllCount();
    this.container = null;
}

Counter.prototype.init = function () {
    var that = this;
    var itemMax = this.itemMax;
    var configs = Array.apply(null, {
        length: 24
    }).map(function (_, i) {
        return {
            val: that.items[i] || 0,
            max: itemMax,
            index: i,
            change: that.change.bind(that)
        };
    });
    this.addControls(configs);
}

Counter.prototype.getAllCount = function () {
    var num = 0;
    var controls = this.controls;
    for (var i = 0, len = controls.length; i < len; i++) {
        num += controls[i].getVal();
    }
    return num;
}

Counter.prototype.getVal = function () {
    var controls = this.controls;
    var data = [];
    for (var i = 0, len = controls.length; i < len; i++) {
        data[data.length] = controls[i].getVal();
    }
    return data;
}

Counter.prototype.resetVal = function (data) {
    var controls = this.controls;
    for (var i = 0, len = controls.length; i < len; i++) {
        controls[i].setVal(data[i] || 0);
    }
}


Counter.prototype.change = function (num, control) {
    var leftNum = this.count - this.getAllCount();
    var max = this.count - (this.getAllCount() - control.val);
    if (leftNum >= 0) {
        this.changeCb(leftNum);
        control.setEnableIncrease(true);
    } else {
        control.setVal(max);
        control.setEnableIncrease(false);
    }
}

Counter.prototype.addControls = function (configs) {
    for (var i = 0, len = configs.length; i < len; i++) {
        this.controls.push(new NumSlider(configs[i]));
    }
}

Counter.prototype.render = function (container) {
    this.init();
    this.container = container;
    var controls = this.controls;
    for (var i = 0, len = controls.length; i < len; i++) {
        controls[i].render(container);
    }
}

// 小数轴

function NumSlider(config) {
    this.container = null;
    this.config = $.extend({}, config, {
        change: this.change.bind(this)
    });
    this.changeCb = config.change;
    this.val = config.initVal || 0;
}

NumSlider.prototype.addEvent = function () {

}

NumSlider.prototype.setEnableIncrease = function (enable) {
    this.container.attr("enableInc", enable)
}

NumSlider.prototype.change = function (val) {
    this.val = +val;
    typeof this.changeCb === 'function' && this.changeCb(val, this);
}

NumSlider.prototype.setVal = function (val) {
    this.container[0].value = val;
    this.val = val;
    this.container.trigger('change');
}

NumSlider.prototype.getVal = function () {
    return this.val;
}

NumSlider.prototype.render = function (wrapper) {
    var config = this.config;
    var val = +this.config.val || 0;
    var sliderWrapper = $("<div class='slider-wrapper'></div>");
    var container = $('<input class="sliderInput" type="text" min="0" max="' + config.max + '" value="' + val + '" >');
    this.val = val;
    container.val(val);
    container.attr("enableInc", true)
    sliderWrapper.append(container);
    wrapper.append(sliderWrapper);
    this.container = sliderWrapper.find(".sliderInput").eq(0);
    container.slider(this.config);
}